package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.IObject;
import objects.Relationship;

public class RelationshipSection implements ISection
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6660630296229107873L;
	private Map<String, Relationship> fRelationshipList;

	public RelationshipSection()
	{
		fRelationshipList = new LinkedHashMap<String, Relationship>();
	}

	public Relationship createNewRelationship(String relationshipName)
	{
		Relationship relationship = new Relationship();

		relationship.setRelationshipName(relationshipName);

		fRelationshipList.put(relationshipName, relationship);

		return relationship;
	}

	public Relationship getRelationship(String relationshipName)
	{
		return fRelationshipList.get(relationshipName);
	}

	private String buildHeaderComment()
	{
		StringBuffer commentBuf = new StringBuffer(900);

		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("# Mapping of physical Matrix Relationships to foreign data tables.  The");
		commentBuf.append(sNewLine);
		commentBuf.append("# following keywords are avalable:");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#   id TABLE(COLUMN)[mapped]");
		commentBuf.append(sNewLine);
		commentBuf.append("#   next EXPRESSION");
		commentBuf.append(sNewLine);
		commentBuf.append("#   attribute name TABLE(COLUMN) [using TABLE(COLUMN)]");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);
		commentBuf.append("################################################################################");
		commentBuf.append(sNewLine);
		commentBuf.append("#");
		commentBuf.append(sNewLine);

		return commentBuf.toString();
	}

	public String buildRelationshipSectionDefinition()
	{
		StringBuffer relSectionBuf = new StringBuffer(fRelationshipList.size() * 1000);

		relSectionBuf.append(buildHeaderComment());
		relSectionBuf.append(sNewLine);

		for (String relName : fRelationshipList.keySet())
		{
			Relationship relationship = fRelationshipList.get(relName);
			relSectionBuf.append(relationship.toString());
			relSectionBuf.append(sNewLine);
		}

		return relSectionBuf.toString();
	}

	@Override
	public String toString()
	{
		return buildRelationshipSectionDefinition();
	}

	@Override
	public int getCount()
	{
		return fRelationshipList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.REL;
	}

	@Override
	public IObject[] getObjects()
	{
		return fRelationshipList.values().toArray(new Relationship[0]);
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void validateState()
	{
		// TODO Auto-generated method stub
		
	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}

	public void removeRelationship(Relationship iObject)
	{
		fRelationshipList.remove(iObject.getObjectName());
	}
}
